package net.sareweb.acab.actions.user;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import javax.faces.event.ActionEvent;

import net.sareweb.acab.components.session.AcabIdentity;
import net.sareweb.acab.components.util.RequestCreationData;
import net.sareweb.acab.entity.Avatar;
import net.sareweb.acab.entity.User;
import net.sareweb.acab.entity.manager.AvatarManager;
import net.sareweb.acab.entity.manager.UserManager;
import net.sareweb.acab.util.AvatarUtil;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.core.Events;
import org.jboss.seam.framework.EntityHome;
import org.jboss.seam.framework.EntityQuery;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;
import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

@Name("userDataBean")

@Scope(ScopeType.CONVERSATION)
public class UserDataBean{
	@Logger 
	private Log log;
	@In(create=true)
	UserManager userManager;
	@In(create=true)
	AvatarManager avatarManager;
	@In 
	Credentials credentials;
    @In 
    AcabIdentity identity;

	
	@Out(value="userData", required=false, scope=ScopeType.CONVERSATION)
	User user;
	
	Avatar avatarBig;
	Avatar avatarMedium;
	Avatar avatarSmall;
	
	private boolean avatarEdited=false;
	

	
	
	
	public String saveUserDataAction(){
		user.setModifyDate(new Date());
		userManager.save(user);
		if(avatarEdited){
			avatarBig.setUser(user);
			avatarMedium.setUser(user);
			avatarSmall.setUser(user);
			
			avatarManager.save(avatarBig);
			avatarManager.save(avatarMedium);
			avatarManager.save(avatarSmall);
			
			//Update avatar in session
			identity.setAvatarBig(avatarBig);
			identity.setAvatarMedium(avatarMedium);
			identity.setAvatarSmall(avatarSmall);
		}
		
		
		return "home";
		
	}




	public void fileUploadListener(UploadEvent event){
		UploadItem item = event.getUploadItem();
		if(avatarBig==null)avatarBig = new Avatar();
		if(avatarMedium==null)avatarMedium = new Avatar();
		if(avatarSmall==null)avatarSmall = new Avatar();
		
		avatarBig.setImage(item.getData().clone());
		avatarBig.setFormat(item.getContentType());
		avatarBig.setType(AvatarManager.ICON_TYPE_BIG);
		avatarBig.setName(item.getFileName());
		item = null;
		avatarEdited=true;
		
		/*TODO: resize avatars whilst is not done, medium and small are copies of big*/
		
		copyAvatarContentAtoB(avatarBig, avatarMedium);
		avatarMedium.setType(AvatarManager.ICON_TYPE_MEDIUM);
		copyAvatarContentAtoB(avatarBig, avatarSmall);
		avatarSmall.setType(AvatarManager.ICON_TYPE_SMALL);
		
	}
	
	public void copyAvatarContentAtoB(Avatar a, Avatar b){
		b.setFormat(a.getFormat());
		b.setImage(a.getImage());
		b.setName(a.getName());
		b.setType(a.getType());
	}
	
	
	



	public Avatar getAvatarBig() {
		return avatarBig;
	}


	public void setAvatarBig(Avatar avatarBig) {
		this.avatarBig = avatarBig;
	}


	public Avatar getAvatarMedium() {
		return avatarMedium;
	}


	public void setAvatarMedium(Avatar avatarMedium) {
		this.avatarMedium = avatarMedium;
	}


	public Avatar getAvatarSmall() {
		return avatarSmall;
	}


	public void setAvatarSmall(Avatar avatarSmall) {
		this.avatarSmall = avatarSmall;
	}


	@Begin
	public String start(){
		return "/acab/user/UserData.xhtml";
	}
	
	@End
	public String end(){
		return "/home.xhtml";
	}
	
	@Factory(value="userData")
	public void instantiateUserData(){
		if(this.user==null){
			log.debug("Instantiating user Data", "");
			this.user= userManager.findByLoginName(credentials.getUsername());
			Set<Avatar> avatars = user.getAvatars();
			if(avatars!=null){
				Iterator<Avatar> it = avatars.iterator();
				while(it.hasNext()){
					Avatar a = it.next();
					if(a.getType().equals(AvatarManager.ICON_TYPE_BIG))avatarBig=a;
					else if(a.getType().equals(AvatarManager.ICON_TYPE_MEDIUM))avatarMedium=a;
					else if(a.getType().equals(AvatarManager.ICON_TYPE_SMALL))avatarSmall=a;
				}
			}
			
		}
	}
	
	
}
